//
//  HUDProgressView.swift
//  BWHUDKit
//
//  Created by zhuxuhong on 2022/3/16.
//

import BWUIKit
import CoreGraphics

public class HUDProgressView: View {
    var value: CGFloat = 0.5 {
        didSet{
            layer.setNeedsDisplay()
        }
    }
    var type: HUD.Square.ProgressType = .ring {
        didSet{
            layer.setNeedsDisplay()
        }
    }
    public var lineWidth: CGFloat = 4 {
        didSet{
            layer.setNeedsDisplay()
        }
    }
    public var trackColor: UIColor = HUD.shared.appearance.tintColor.withAlphaComponent(0.15) {
        didSet{
            layer.setNeedsDisplay()
        }
    }
    public var progressColor: UIColor = HUD.shared.appearance.tintColor {
        didSet{
            layer.setNeedsDisplay()
        }
    }
    
    lazy var trackLayer: CAShapeLayer = {
        let layer = CAShapeLayer()
        layer.fillColor = UIColor.clear.cgColor
        layer.strokeColor = trackColor.cgColor
        layer.lineWidth = lineWidth
        layer.lineCap = .round
        layer.backgroundColor = UIColor.clear.cgColor
        return layer
    }()
    lazy var progressLayer: CAShapeLayer = {
        let layer = CAShapeLayer()
        layer.fillColor = UIColor.clear.cgColor
        layer.strokeColor = progressColor.cgColor
        layer.lineWidth = lineWidth
        layer.lineCap = .round
        layer.backgroundColor = UIColor.clear.cgColor
        return layer
    }()
    
    private func commonInit(){
        backgroundColor = .clear
        layer.addSublayer(trackLayer)
        layer.addSublayer(progressLayer)
    }
    public override func didAwake() {
        commonInit()
    }
    public override func didInit() {
        commonInit()
    }
    
    public override func layoutSubviews() {
        super.layoutSubviews()
        
        trackLayer.frame = layer.bounds
        progressLayer.frame = layer.bounds
    }
    
    public override func draw(_ layer: CALayer, in ctx: CGContext) {
        let center = CGPoint(x: layer.bounds.midX, y: layer.bounds.midY)
        var radius = min(layer.bounds.width, layer.bounds.height)/2 - lineWidth/2
        switch type {
        case .slider:
            let w = layer.bounds.width - lineWidth/2
            let y = center.y - lineWidth/2
            let trackPath = UIBezierPath()
            trackPath.move(to: .init(x: lineWidth/2, y: y))
            trackPath.addLine(to: .init(x: w, y: y))
            trackLayer.path = trackPath.cgPath
            
            progressLayer.fillColor = UIColor.clear.cgColor
            progressLayer.strokeColor = HUD.shared.appearance.tintColor.cgColor
            
            let path = UIBezierPath()
            path.move(to: .init(x: lineWidth/2, y: y))
            path.addLine(to: .init(x: w*value, y: y))
            progressLayer.path = path.cgPath
            
        case .ring, .pie, .water:
            let trackPath = UIBezierPath(arcCenter: center, radius: radius, startAngle: 0, endAngle: .pi*2, clockwise: true)
            trackLayer.path = trackPath.cgPath
            
            switch type {
            case .ring:
                progressLayer.fillColor = UIColor.clear.cgColor
                progressLayer.strokeColor = HUD.shared.appearance.tintColor.cgColor
                
                let path = UIBezierPath(arcCenter: center, radius: radius, startAngle: -.pi/2, endAngle: -.pi/2*(1-value) + 3*(.pi/2)*value, clockwise: true)
                progressLayer.path = path.cgPath
            
            case .pie:
                progressLayer.fillColor = HUD.shared.appearance.tintColor.cgColor
                progressLayer.strokeColor = nil
                
                let path = UIBezierPath()
                path.move(to: center)
                radius = min(layer.bounds.width, layer.bounds.height)/2 - lineWidth
                path.addArc(withCenter: center, radius: radius, startAngle: -.pi/2, endAngle: -.pi/2*(1-value) + 3*(.pi/2)*value, clockwise: true)
                path.close()
                progressLayer.path = path.cgPath
            
            case .water:
                progressLayer.fillColor = HUD.shared.appearance.tintColor.cgColor
                progressLayer.strokeColor = nil
                
                let path = UIBezierPath()
                radius = min(layer.bounds.width, layer.bounds.height)/2 - lineWidth
                
                let halfAngle: CGFloat = 2 * .pi * value / 2
                let startAngle: CGFloat = .pi/2 - halfAngle
                let endAngle: CGFloat = .pi/2 + halfAngle
                
                path.addArc(withCenter: center, radius: radius, startAngle: startAngle, endAngle: endAngle, clockwise: true)
                
                path.close()
                progressLayer.path = path.cgPath
                
            default: break
            }
        }
        trackLayer.render(in: ctx)
        progressLayer.render(in: ctx)
    }
}
